package cn.swust.designpatternlearningplatform.controller;

import cn.swust.designpatternlearningplatform.common.Result.R;
import cn.swust.designpatternlearningplatform.common.utils.AuthUserContext;
import cn.swust.designpatternlearningplatform.domain.dto.ShowStudentDto;
import cn.swust.designpatternlearningplatform.domain.dto.TestPaperDto;
import cn.swust.designpatternlearningplatform.domain.dto.UserInfoDto;
import cn.swust.designpatternlearningplatform.service.AboutService;
import cn.swust.designpatternlearningplatform.service.StudentTrainingPaperService;
import cn.swust.designpatternlearningplatform.service.TeacherTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {
    @Resource
    private TeacherTestService teacherTestService;
    @Resource
    private StudentTrainingPaperService studentTrainingPaperService;

    @Resource
    private AboutService aboutService;
    /**
     * @param testId:
     * @return R<Object>
     * @author wang
     * description 展示试题详细信息
     * date 2023/5/28 15:53
     */
    @RequestMapping("/testListDetails")
    public R<Object> testListDetails(@RequestParam("testId") String testId ,@RequestParam("type") String type){
        if (Objects.equals(type, "测试卷")){
            String number= AuthUserContext.get().getNumber();
            String role=AuthUserContext.get().getRole();
            TestPaperDto testPaperDto=new TestPaperDto();
            if(Objects.equals(role, "student")){
                log.info(number);
                testPaperDto=teacherTestService.selectTestPaper(testId,number);
            }
            else {
                number="";
                testPaperDto=teacherTestService.selectTestPaper(testId,number);
            }
            if (testPaperDto==null)
            {
                return R.success("该试卷已经做过，不可重复提交");
            }
            return R.success(testPaperDto);
        }
        else{
            String number= AuthUserContext.get().getNumber();
            TestPaperDto testPaperDto=studentTrainingPaperService.selectTrainingPaper(testId,number);
            return R.success(testPaperDto);
        }

    }
    /**
     * @param :
      * @return R<Object>
     * @author wang
     * description 登陆时获取信息进行判断
     * date 2023/7/17 15:46
     */
    @RequestMapping("/getUserInfo")
    public R<Object> getUserInfo(){
        String userName=AuthUserContext.get().getName();
        String userCount=AuthUserContext.get().getNumber();
        String role=AuthUserContext.get().getRole();
        UserInfoDto userInfoDto=new UserInfoDto();
        userInfoDto.setUserName(userName);
        userInfoDto.setUserCount(userCount);
        userInfoDto.setRole(role);
        return R.success(userInfoDto);
    }


    @RequestMapping("/lookAbout")
    public R selectAbout(){
        return aboutService.selectAll();
    }


}
